package com.example.oracleviewer.repository;

import com.example.oracleviewer.entity.UserCard;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户卡片数据访问接口
 */
@Repository
public interface UserCardRepository extends JpaRepository<UserCard, String> {
    
    /**
     * 查询所有用户卡片数据
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all", nativeQuery = true)
    List<UserCard> findAllUserCards();
    
    /**
     * 根据用户ID查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE USERID = :userId", nativeQuery = true)
    UserCard findByUserId(@Param("userId") String userId);
    
    /**
     * 根据更新时间大于指定时间查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE UPDATETIME > TO_DATE(:time, 'YYYY-MM-DD HH24:MI:SS') ORDER BY UPDATETIME ASC", nativeQuery = true)
    List<UserCard> findByUpdateTimeGreaterThan(@Param("time") String time);
    
    /**
     * 根据更新时间大于指定时间查询（使用TO_TIMESTAMP）
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE UPDATETIME > TO_TIMESTAMP(:time, 'YYYY-MM-DD HH24:MI:SS') ORDER BY UPDATETIME ASC", nativeQuery = true)
    List<UserCard> findByUpdateTimeGreaterThanTimestamp(@Param("time") String time);
    
    /**
     * 根据用户编号查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE USERNUMB = :userNumb", nativeQuery = true)
    List<UserCard> findByUserNumb(@Param("userNumb") String userNumb);
    
    /**
     * 根据卡片号查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE CARDNO = :cardNo", nativeQuery = true)
    List<UserCard> findByCardNo(@Param("cardNo") Integer cardNo);
    
    /**
     * 根据用户状态查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE USERSTATUS = :userStatus", nativeQuery = true)
    List<UserCard> findByUserStatus(@Param("userStatus") Integer userStatus);
    
    /**
     * 根据卡片状态查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE CARDSTATUS = :cardStatus", nativeQuery = true)
    List<UserCard> findByCardStatus(@Param("cardStatus") Integer cardStatus);
    
    /**
     * 根据部门名称查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE DEPTNAME LIKE '%' || :deptName || '%'", nativeQuery = true)
    List<UserCard> findByDeptNameContaining(@Param("deptName") String deptName);
    
    /**
     * 根据用户名查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE USERNAME LIKE '%' || :username || '%'", nativeQuery = true)
    List<UserCard> findByUsernameContaining(@Param("username") String username);
    
    /**
     * 根据工厂固定ID查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE FACTORYFIXID = :factoryFixId", nativeQuery = true)
    List<UserCard> findByFactoryFixId(@Param("factoryFixId") String factoryFixId);
    
    /**
     * 根据类别查询
     */
    @Query(value = "SELECT * FROM ECARD_V3.sync_usercard_all WHERE CATEGORY = :category", nativeQuery = true)
    List<UserCard> findByCategory(@Param("category") Integer category);
} 